﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYElectricCommon;
using LampUseFactorManager;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class DlgLightSourceLibrary : System.Windows.Forms.Form
	{
		public DlgLightSourceLibrary()
		{
			this.InitializeComponent();
			this.dgvLampLibrary.Controls.Add(this.cbxDefaultLight);
			this.dgvLampLibrary.Controls.Add(this.btnName);
		}

		private void DlgLightSourceLibrary_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			string str = Path.Combine(Product.WorkingLocation, "IlluminanceCal.db3");
			HYElectricCommon.SqLiteHelper.SqlLiteDatabase("Data Source=" + str + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10");
			this.FillSelection();
		}

		private void tabControl1_Selected(object sender, TabControlEventArgs e)
		{
			if (this.tabControl1.SelectedTab == this.pageLightSource)
			{
				this.FillSelection();
				return;
			}
			if (this.tabControl1.SelectedTab == this.pageLamp)
			{
				this.cbxLampType.Items.Clear();
				System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select [LampType] from [LampType]", Array.Empty<object>()));
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					this.cbxLampType.Items.Add(dataTable.Rows[i]["LampType"]);
				}
				this.cbxLampType.SelectedIndex = 0;
				this.comfirmClbValue();
			}
		}

		private void FillSelection()
		{
			this.cbxSelectByType.Items.Clear();
			System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select LightSourceType from [LightSourceType]", Array.Empty<object>()));
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				this.cbxSelectByType.Items.Add(dataTable.Rows[i]["LightSourceType"]);
				this.LampType.Items.Add(dataTable.Rows[i]["LightSourceType"]);
			}
			this.cbxSelectByFactory.Items.Clear();
			System.Data.DataTable dataTable2 = HYElectricCommon.Database.ExecQuery(string.Format("select DISTINCT Factory from [LightSource]", Array.Empty<object>()));
			for (int j = 0; j < dataTable2.Rows.Count; j++)
			{
				this.cbxSelectByFactory.Items.Add(dataTable2.Rows[j]["Factory"]);
			}
			if (this.cbxSelectByFactory.Items.Count > 0)
			{
				this.cbxSelectByFactory.SelectedIndex = 0;
			}
			if (this.cbxSelectByType.Items.Count > 0)
			{
				this.cbxSelectByType.SelectedIndex = 0;
			}
		}

		private void rdbType_CheckedChanged(object sender, EventArgs e)
		{
			this.cbxSelectByType.Enabled = this.rdbType.Checked;
			this.cbxSelectByFactory.Enabled = this.rdbFactory.Checked;
			System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select b.[ID],a.[LightSourceType], b.[Model] ,b.[LuminousFlux] ,b.[Power] ,b.[Factory] ,b.[Name]  from [LightSource] as b inner join [LightSourceType] as a on a.[ID]=b.[LightSourceTypeID] where a.[LightSourceType]='{0}'", this.cbxSelectByType.SelectedItem.ToString()));
			this.dgvLightSourceLibrary.DataSource = dataTable;
			dataTable.AcceptChanges();
		}

		private void rdbFactory_CheckedChanged(object sender, EventArgs e)
		{
			this.cbxSelectByType.Enabled = this.rdbType.Checked;
			this.cbxSelectByFactory.Enabled = this.rdbFactory.Checked;
			this.dgvLightSourceLibrary.DataSource = HYElectricCommon.Database.ExecQuery(string.Format("select b.[ID],a.[LightSourceType], b.[Model] ,b.[LuminousFlux] ,b.[Power] ,b.[Factory] ,b.[Name]  from [LightSource] as b inner join [LightSourceType] as a on a.[ID]=b.[LightSourceTypeID] where b.[Factory]='{0}'", this.cbxSelectByFactory.SelectedItem.ToString()));
			((DataTable)this.dgvLightSourceLibrary.DataSource).AcceptChanges();
		}

		private void cbxSelectByType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SelectType();
		}

		private void SelectType()
		{
			if (this.cbxSelectByType.SelectedItem != null)
			{
				this.dgvLightSourceLibrary.DataSource = HYElectricCommon.Database.ExecQuery(string.Format("select b.[ID],a.[LightSourceType], b.[Model] ,b.[LuminousFlux] ,b.[Power] ,b.[Factory] ,b.[Name]  from [LightSource] as b inner join [LightSourceType] as a on a.[ID]=b.[LightSourceTypeID] where a.[LightSourceType]='{0}'", this.cbxSelectByType.SelectedItem.ToString()));
				((DataTable)this.dgvLightSourceLibrary.DataSource).AcceptChanges();
			}
		}

		private void cbxSelectByFactory_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SelectFactory();
		}

		private void SelectFactory()
		{
			if (this.cbxSelectByFactory.SelectedItem != null)
			{
				this.dgvLightSourceLibrary.DataSource = HYElectricCommon.Database.ExecQuery(string.Format("select b.[ID],a.[LightSourceType], b.[Model] ,b.[LuminousFlux] ,b.[Power] ,b.[Factory] ,b.[Name]  from [LightSource] as b inner join [LightSourceType] as a on a.[ID]=b.[LightSourceTypeID] where b.[Factory]='{0}'", this.cbxSelectByFactory.SelectedItem.ToString()));
				((DataTable)this.dgvLightSourceLibrary.DataSource).AcceptChanges();
			}
		}

		private int GetLampTypeId(string typeName)
		{
			return Convert.ToInt32(HYElectricCommon.Database.ExecQuery(string.Format("select [ID] from [LightSourceType] where [LightSourceType]='{0}'", typeName)).Rows[0][0]);
		}

		private int GetLampTypeId1(string typeName)
		{
			return Convert.ToInt32(HYElectricCommon.Database.ExecQuery(string.Format("select [ID] from [LampType] where [LampType]='{0}'", typeName)).Rows[0][0]);
		}

		private void dgvLightSourceLibrary_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (this.tabControl1.SelectedTab == this.pageLightSource && this.dgvLightSourceLibrary.Rows.Count > 1)
			{
				try
				{
					try
					{
						for (int i = 0; i < this.dgvLightSourceLibrary.RowCount - 1; i++)
						{
							this.GetLampTypeId(this.dgvLightSourceLibrary.Rows[i].Cells["LampType"].Value.ToString());
						}
					}
					catch (Exception)
					{
						MessageBox.Show("请选择光源类别。", "盈建科提示");
						return;
					}
					bool flag = false;
					for (int j = 0; j < this.dgvLightSourceLibrary.RowCount - 1; j++)
					{
						int lampTypeId = this.GetLampTypeId(this.dgvLightSourceLibrary.Rows[j].Cells["LampType"].Value.ToString());
						DataRow row = (this.dgvLightSourceLibrary.Rows[j].DataBoundItem as DataRowView).Row;
						if (row.RowState == DataRowState.Modified)
						{
							HYElectricCommon.Database.ExecQuery(string.Format("update [LightSource] set [LightSourceTypeID]='{0}',[Model]='{1}',[LuminousFlux]='{2}',[Power]='{3}',[Factory]='{4}',[Name]='{5}' where [ID]='{6}'", new object[]
							{
								lampTypeId,
								row["Model"].ToString(),
								row["LuminousFlux"].ToString(),
								row["Power"].ToString(),
								row["Factory"].ToString(),
								row["Name"].ToString(),
								row["ID"].ToString()
							}));
							flag = true;
						}
						else if (row.RowState == DataRowState.Added)
						{
							HYElectricCommon.Database.ExecQuery(string.Format("insert into [LightSource] ([LightSourceTypeID],[Model],[LuminousFlux],[Power],[Factory],[Name]) values('{0}','{1}','{2}','{3}','{4}','{5}')", new object[]
							{
								lampTypeId,
								row["Model"].ToString(),
								row["LuminousFlux"].ToString(),
								row["Power"].ToString(),
								row["Factory"].ToString(),
								row["Name"].ToString()
							}));
							flag = true;
						}
					}
					if (flag)
					{
						string selectedItem = this.cbxSelectByFactory.SelectedItem.ToString();
						this.cbxSelectByFactory.Items.Clear();
						System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select DISTINCT Factory from [LightSource]", Array.Empty<object>()));
						for (int k = 0; k < dataTable.Rows.Count; k++)
						{
							this.cbxSelectByFactory.Items.Add(dataTable.Rows[k]["Factory"]);
						}
						this.cbxSelectByFactory.SelectedItem = selectedItem;
					}
					if (this.rdbType.Checked)
					{
						this.SelectType();
					}
					else
					{
						this.SelectFactory();
					}
					MessageBox.Show("保存成功。", "盈建科提示");
					return;
				}
				catch (Exception)
				{
					MessageBox.Show("提交发生异常！请检查录入的数据是否有重复。", "盈建科提示");
					return;
				}
			}
			if (this.tabControl1.SelectedTab == this.pageLamp && this.dgvLampLibrary.Rows.Count > 1)
			{
				try
				{
					try
					{
						for (int l = 0; l < this.dgvLampLibrary.Rows.Count - 1; l++)
						{
							this.getLampTypeIds(this.dgvLampLibrary.Rows[l].Cells["LampTypeID"].Value.ToString());
							if (this.getLightSrcId(this.dgvLampLibrary.Rows[l].Cells["DefaultLightSrc"].Value.ToString()) == -1)
							{
								MessageBox.Show("默认光源不能为空。", "盈建科提示");
								return;
							}
						}
					}
					catch (Exception)
					{
						MessageBox.Show("灯具类别和默认光源不能为空。", "盈建科提示");
						return;
					}
					for (int m = 0; m < this.dgvLampLibrary.Rows.Count - 1; m++)
					{
						string lampTypeIds = this.getLampTypeIds(this.dgvLampLibrary.Rows[m].Cells["LampTypeID"].Value.ToString());
						int num = (this.dgvLampLibrary.Rows[m].Cells["DefaultLightSrc"].Value == null) ? -1 : this.getLightSrcId(this.dgvLampLibrary.Rows[m].Cells["DefaultLightSrc"].Value.ToString().Trim());
						string text = (this.dgvLampLibrary.Rows[m].Cells["UtilizationFactor"].Value == null) ? "" : this.dgvLampLibrary.Rows[m].Cells["UtilizationFactor"].Value.ToString();
						string text2 = (this.dgvLampLibrary.Rows[m].Cells["Factory"].Value == null) ? "" : this.dgvLampLibrary.Rows[m].Cells["Factory"].Value.ToString();
						string text3 = (this.dgvLampLibrary.Rows[m].Cells["Name"].Value == null) ? "" : this.dgvLampLibrary.Rows[m].Cells["Name"].Value.ToString();
						string text4 = (this.dgvLampLibrary.Rows[m].Cells["Style"].Value == null) ? "" : this.dgvLampLibrary.Rows[m].Cells["Style"].Value.ToString();
						string text5 = (this.dgvLampLibrary.Rows[m].Cells["LampEfficiency"].Value == null) ? "0" : this.dgvLampLibrary.Rows[m].Cells["LampEfficiency"].Value.ToString();
						string text6 = (this.dgvLampLibrary.Rows[m].Cells["MaxJGBAA"].Value == null) ? "0" : this.dgvLampLibrary.Rows[m].Cells["MaxJGBAA"].Value.ToString();
						string text7 = (this.dgvLampLibrary.Rows[m].Cells["MaxJGBBB"].Value == null) ? "0" : this.dgvLampLibrary.Rows[m].Cells["MaxJGBBB"].Value.ToString();
						string text8 = (this.dgvLampLibrary.Rows[m].Cells["LightSrcCount"].Value == null) ? "0" : this.dgvLampLibrary.Rows[m].Cells["LightSrcCount"].Value.ToString();
						string text9 = (this.dgvLampLibrary.Rows[m].Cells["BallastCount"].Value == null) ? "0" : this.dgvLampLibrary.Rows[m].Cells["BallastCount"].Value.ToString();
						string text10 = (this.dgvLampLibrary.Rows[m].Cells["BallastPower"].Value == null) ? "0" : this.dgvLampLibrary.Rows[m].Cells["BallastPower"].Value.ToString();
						int num2 = Convert.ToBoolean(this.dgvLampLibrary.Rows[m].Cells["IsStore"].Value) ? 1 : 0;
						if (this.dgvLampLibrary.Rows[m].Cells["lampID"].Value != null && this.dgvLampLibrary.Rows[m].Cells["lampID"].Value.ToString() != "")
						{
							HYElectricCommon.Database.ExecuteCommand(string.Format("update [Lamp] set [Name]='{0}',[Model]='{1}',[Efficiency]='{2}',[MaxLHA]='{3}',[MaxLHB]='{4}',[LampTypeID]='{5}',[DefaultLightSourceID]='{6}',[LightSourceCount]='{7}',[BallastCount]='{8}',[BallastPower]='{9}',[Factory]='{10}',[UseFactorTable]='{11}',[IsStore]={12} where [ID]='{13}'", new object[]
							{
								text3,
								text4,
								text5,
								text6,
								text7,
								lampTypeIds,
								num,
								text8,
								text9,
								text10,
								text2,
								text,
								num2,
								this.dgvLampLibrary.Rows[m].Cells["lampID"].Value.ToString()
							}));
						}
						else
						{
							HYElectricCommon.Database.ExecuteCommand(string.Format("insert into [Lamp] ([Name],[Model],[Efficiency],[MaxLHA],[MaxLHB],[LampTypeID],[DefaultLightSourceID],[LightSourceCount],[BallastCount],[BallastPower],[Factory],[UseFactorTable],[IsStore]) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}',{12})", new object[]
							{
								text3,
								text4,
								text5,
								text6,
								text7,
								lampTypeIds,
								num,
								text8,
								text9,
								text10,
								text2,
								text,
								num2
							}));
						}
					}
					if (this.cbxLampType.SelectedItem.ToString() == "常用灯具")
					{
						this.GetLamp(true);
					}
					else
					{
						this.GetLamp(false);
					}
					MessageBox.Show("提交成功。");
				}
				catch (Exception)
				{
					MessageBox.Show("提交发生异常！请检查录入的数据是否有重复。");
				}
			}
		}

		private string GetLampNamesTypes(string[] typeIds)
		{
			string text = HYElectricCommon.Database.ExecQuery(string.Format("select [LampType] from [LampType] where [id]='{0}'", typeIds[0])).Rows[0][0].ToString();
			if (typeIds.Count<string>() > 1)
			{
				for (int i = 1; i < typeIds.Count<string>(); i++)
				{
					text = text + "," + HYElectricCommon.Database.ExecQuery(string.Format("select [LampType] from [LampType] where [id]='{0}'", typeIds[i])).Rows[0][0].ToString();
				}
			}
			return text;
		}

		private string getLampTypeIds(string typeNames)
		{
			string[] array = typeNames.Split(new char[]
			{
				','
			});
			List<string> list = new List<string>();
			for (int i = 0; i < array.Count<string>(); i++)
			{
				list.Add(this.GetLampTypeId1(array[i]).ToString().Trim());
			}
			return string.Join(",", list);
		}

		private int getLightSrcId(string strSrc)
		{
			if (!(strSrc != ""))
			{
				return -1;
			}
			string[] array = strSrc.Split(new char[]
			{
				':'
			});
			System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select [ID] from [LightSource] where [Model]='{0}' and [Name]='{1}'", array[1].Trim(), array[0].Trim()));
			if (dataTable.Rows.Count > 0)
			{
				return Convert.ToInt32(dataTable.Rows[0][0]);
			}
			return -1;
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.tabControl1.SelectedTab == this.pageLightSource)
				{
					if (this.dgvLightSourceLibrary.SelectedRows[0].Index != this.dgvLightSourceLibrary.Rows.Count - 1 && DialogResult.Yes == MessageBox.Show("是否要将选中项删除？", "盈建科提示", MessageBoxButtons.YesNo))
					{
						foreach (object obj in this.dgvLightSourceLibrary.SelectedRows)
						{
							DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
							if (dataGridViewRow.Cells["ID"].Value != null && dataGridViewRow.Cells["ID"].Value.ToString() != "")
							{
								HYElectricCommon.Database.ExecQuery(string.Format("delete from [LightSource] where [ID]='{0}'", dataGridViewRow.Cells["ID"].Value.ToString()));
								this.dgvLightSourceLibrary.Rows.Remove(dataGridViewRow);
							}
							else
							{
								this.dgvLightSourceLibrary.Rows.Remove(dataGridViewRow);
							}
						}
						this.dgvLightSourceLibrary.Refresh();
					}
				}
				else if (this.tabControl1.SelectedTab == this.pageLamp && this.dgvLampLibrary.SelectedRows[0].Index != this.dgvLampLibrary.Rows.Count - 1 && DialogResult.Yes == MessageBox.Show("是否要将选中项删除？", "照度计算数据库管理器", MessageBoxButtons.YesNo))
				{
					foreach (object obj2 in this.dgvLampLibrary.SelectedRows)
					{
						DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
						if (dataGridViewRow2.Cells["lampID"].Value != null && dataGridViewRow2.Cells["lampID"].Value.ToString() != "")
						{
							HYElectricCommon.Database.ExecuteCommand(string.Format("delete from [Lamp] where [ID]='{0}'", dataGridViewRow2.Cells["lampID"].Value.ToString()));
							this.dgvLampLibrary.Rows.Remove(dataGridViewRow2);
						}
						else
						{
							this.dgvLampLibrary.Rows.Remove(dataGridViewRow2);
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void cbxLampType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cbxLampType.SelectedItem != null)
			{
				if (this.cbxLampType.SelectedItem.ToString() == "常用灯具")
				{
					this.GetLamp(true);
					return;
				}
				this.GetLamp(false);
			}
		}

		private void GetLamp(bool isUsed)
		{
			this.dgvLampLibrary.Rows.Clear();
			System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select [ID] from [LampType] where [LampType]='{0}'", this.cbxLampType.SelectedItem.ToString()));
			if (dataTable.Rows.Count > 0)
			{
				System.Data.DataTable dataTable2 = HYElectricCommon.Database.ExecQuery(string.Format("select [ID],[Name],[Model],[Efficiency],[MaxLHA],[MaxLHB],[LampTypeID],[DefaultLightSourceID],[LightSourceCount],[BallastCount],[BallastPower],[Factory],[UseFactorTable],[IsStore] from [Lamp]", Array.Empty<object>()));
				if (dataTable2.Rows.Count > 0)
				{
					if (isUsed)
					{
						for (int i = 0; i < dataTable2.Rows.Count; i++)
						{
							bool flag = Convert.ToBoolean(dataTable2.Rows[i][13]);
							if (flag)
							{
								string typeIds = dataTable2.Rows[i][6].ToString().Trim();
								string lampTypes = this.GetLampTypes(typeIds);
								string defaultLightSrc = this.GetDefaultLightSrc(Convert.ToInt32(dataTable2.Rows[i][7]));
								this.dgvLampLibrary.Rows.Add(new object[]
								{
									dataTable2.Rows[i][0].ToString(),
									dataTable2.Rows[i][1].ToString(),
									dataTable2.Rows[i][2].ToString(),
									dataTable2.Rows[i][3].ToString(),
									dataTable2.Rows[i][4].ToString(),
									dataTable2.Rows[i][5].ToString(),
									lampTypes,
									defaultLightSrc,
									dataTable2.Rows[i][8].ToString(),
									dataTable2.Rows[i][9].ToString(),
									dataTable2.Rows[i][10].ToString(),
									dataTable2.Rows[i][11].ToString(),
									dataTable2.Rows[i][12],
									flag
								});
							}
						}
						return;
					}
					string b = dataTable.Rows[0][0].ToString();
					for (int j = 0; j < dataTable2.Rows.Count; j++)
					{
						string text = dataTable2.Rows[j][6].ToString().Trim();
						if (text == b)
						{
							string lampTypes2 = this.GetLampTypes(text);
							string defaultLightSrc2 = this.GetDefaultLightSrc(Convert.ToInt32(dataTable2.Rows[j][7]));
							bool flag2 = Convert.ToBoolean(dataTable2.Rows[j][13]);
							this.dgvLampLibrary.Rows.Add(new object[]
							{
								dataTable2.Rows[j][0].ToString(),
								dataTable2.Rows[j][1].ToString(),
								dataTable2.Rows[j][2].ToString(),
								dataTable2.Rows[j][3].ToString(),
								dataTable2.Rows[j][4].ToString(),
								dataTable2.Rows[j][5].ToString(),
								lampTypes2,
								defaultLightSrc2,
								dataTable2.Rows[j][8].ToString(),
								dataTable2.Rows[j][9].ToString(),
								dataTable2.Rows[j][10].ToString(),
								dataTable2.Rows[j][11].ToString(),
								dataTable2.Rows[j][12],
								flag2
							});
						}
					}
				}
			}
		}

		private string GetLampTypes(string typeIds)
		{
			return HYElectricCommon.Database.ExecQuery(string.Format("select [LampType] from [LampType] where [id]='{0}'", typeIds)).Rows[0][0].ToString();
		}

		private string GetDefaultLightSrc(int id)
		{
			string result = "";
			System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select [Name],[Model] from [LightSource] where [ID]='{0}'", id));
			if (dataTable.Rows.Count > 0)
			{
				result = dataTable.Rows[0][0].ToString().Trim() + ":" + dataTable.Rows[0][1].ToString().Trim();
			}
			return result;
		}

		private void dataGridView2_CellEnter(object sender, DataGridViewCellEventArgs e)
		{
			DataGridViewCell dataGridViewCell = this.dgvLampLibrary.Rows[e.RowIndex].Cells[e.ColumnIndex];
			System.Drawing.Rectangle cellDisplayRectangle = this.dgvLampLibrary.GetCellDisplayRectangle(dataGridViewCell.ColumnIndex, dataGridViewCell.RowIndex, false);
			if (e.ColumnIndex == 7)
			{
				object value = this.dgvLampLibrary.Rows[e.RowIndex].Cells[7].Value;
				string lampTypeNames = (value != null) ? value.ToString() : "";
				this.InitCbxDefSrc(lampTypeNames);
				this.cbxDefaultLight.Location = cellDisplayRectangle.Location;
				this.cbxDefaultLight.Size = cellDisplayRectangle.Size;
				this.comfirmComboBoxValue(this.cbxDefaultLight, dataGridViewCell.Value as string);
				this.cbxDefaultLight.Visible = true;
				return;
			}
			if (e.ColumnIndex == 12)
			{
				System.Drawing.Point location = cellDisplayRectangle.Location;
				location.X = location.X + cellDisplayRectangle.Width - this.btnName.Width;
				this.btnName.Location = location;
				this.btnName.Visible = true;
			}
		}

		private void InitCbxDefSrc(string lampTypeNames)
		{
			this.cbxDefaultLight.Items.Clear();
			List<string> defaultLightSrcs = this.getDefaultLightSrcs(this.getLightSourceTypeByName(lampTypeNames));
			for (int i = 0; i < defaultLightSrcs.Count; i++)
			{
				this.cbxDefaultLight.Items.Add(defaultLightSrcs[i]);
			}
			if (this.cbxDefaultLight.Items.Count > 0)
			{
				this.cbxDefaultLight.SelectedIndex = 0;
				this.setCbxWidth(this.cbxDefaultLight);
			}
		}

		private int getLightSourceTypeByName(string strSrc)
		{
			if (!(strSrc != ""))
			{
				return -1;
			}
			string[] array = strSrc.Split(new char[]
			{
				':'
			});
			System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select [LightSourceTypeID] from [LightSource] where [Model]='{0}' and [Name]='{1}'", array[1].Trim(), array[0].Trim()));
			if (dataTable.Rows.Count > 0)
			{
				return Convert.ToInt32(dataTable.Rows[0][0]);
			}
			return -1;
		}

		private void comfirmClbValue()
		{
			this.LampTypeID.Items.Clear();
			System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select [LampType] from [LampType]", Array.Empty<object>()));
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				if (dataTable.Rows[i]["LampType"].ToString() != "常用灯具")
				{
					this.LampTypeID.Items.Add(dataTable.Rows[i]["LampType"]);
				}
			}
		}

		private void dataGridView2_CellLeave(object sender, DataGridViewCellEventArgs e)
		{
			DataGridViewCell dataGridViewCell = this.dgvLampLibrary.Rows[e.RowIndex].Cells[e.ColumnIndex];
			if (e.ColumnIndex == 7)
			{
				dataGridViewCell.Value = this.cbxDefaultLight.Text;
				this.cbxDefaultLight.Visible = false;
				return;
			}
			if (e.ColumnIndex == 12)
			{
				this.btnName.Visible = false;
			}
		}

		private void comfirmComboBoxValue(ComboBox com, string cellValue)
		{
			com.SelectedIndex = -1;
			if (cellValue == null)
			{
				com.Text = "";
				return;
			}
			com.Text = cellValue;
			foreach (object obj in com.Items)
			{
				if ((string)obj == cellValue)
				{
					com.SelectedItem = obj;
				}
			}
		}

		private void setCbxWidth(ComboBox cbx)
		{
			int num = 0;
			Graphics graphics = base.CreateGraphics();
			for (int i = 0; i < cbx.Items.Count; i++)
			{
				cbx.SelectedIndex = i;
				SizeF sizeF = graphics.MeasureString(cbx.Text, cbx.Font);
				if (num < (int)sizeF.Width)
				{
					num = (int)sizeF.Width;
				}
			}
			cbx.DropDownWidth = cbx.DropDownWidth;
			if (cbx.DropDownWidth < num)
			{
				cbx.DropDownWidth = num;
			}
		}

		private List<string> getDefaultLightSrcs(int lightSourceTypeID)
		{
			List<string> list = new List<string>();
			if (lightSourceTypeID == -1)
			{
				object value = this.dgvLampLibrary.CurrentRow.Cells[6].Value;
				string text = (value != null) ? value.ToString() : "";
				if (text != "")
				{
					System.Data.DataTable dataTable = HYElectricCommon.Database.ExecQuery(string.Format("select [DefaultLightSourceType] from [LampType] where [LampType]='{0}'", text));
					if (dataTable.Rows.Count > 0)
					{
						foreach (string arg in dataTable.Rows[0][0].ToString().Split(new char[]
						{
							','
						}))
						{
							System.Data.DataTable dataTable2 = HYElectricCommon.Database.ExecQuery(string.Format("select [Name],[Model] from [LightSource] where [lightSourceTypeID]={0}", arg));
							for (int j = 0; j < dataTable2.Rows.Count; j++)
							{
								list.Add(dataTable2.Rows[j][0].ToString().Trim() + ":" + dataTable2.Rows[j][1].ToString().Trim());
							}
						}
						return list;
					}
				}
			}
			else
			{
				System.Data.DataTable dataTable3 = HYElectricCommon.Database.ExecQuery(string.Format("select [Name],[Model] from [LightSource] where [lightSourceTypeID]={0}", lightSourceTypeID));
				for (int k = 0; k < dataTable3.Rows.Count; k++)
				{
					list.Add(dataTable3.Rows[k][0].ToString().Trim() + ":" + dataTable3.Rows[k][1].ToString().Trim());
				}
			}
			return list;
		}

		private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
		}

		private void dataGridView2_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
		{
			this.cbxDefaultLight.Visible = false;
			this.btnName.Visible = false;
		}

		private void dataGridView2_Scroll(object sender, ScrollEventArgs e)
		{
			this.cbxDefaultLight.Visible = false;
			this.btnName.Visible = false;
		}

		private void btnName_Click(object sender, EventArgs e)
		{
			DatabaseType dbType = DatabaseType.Local;
			int rowIndex = this.dgvLampLibrary.CurrentCell.RowIndex;
			string str = AssistFunc.GetAppRootPath() + "\\Data\\LampUseFactor.db";
			string connStr = "Data Source=" + str + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
			string utilizationFactorTableName;
			if (this.dgvLampLibrary.Rows[rowIndex].Cells[0].Value != null && this.dgvLampLibrary.Rows[rowIndex].Cells[0].Value.ToString() != "")
			{
				int lampId = Convert.ToInt32(this.dgvLampLibrary.Rows[rowIndex].Cells[0].Value.ToString());
				utilizationFactorTableName = LampUseFactorManager.UtilizationFactor.GetUtilizationFactorTableName(dbType, connStr, lampId);
			}
			else
			{
				utilizationFactorTableName = LampUseFactorManager.UtilizationFactor.GetUtilizationFactorTableName(dbType, connStr);
			}
			if (!string.IsNullOrEmpty(utilizationFactorTableName))
			{
				this.dgvLampLibrary.Rows[rowIndex].Cells[12].Value = utilizationFactorTableName;
				this.btnName.Parent.Focus();
				this.dgvLampLibrary.RefreshEdit();
			}
		}

		private void dgvLightSourceLibrary_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
		}
	}
}
